<?php
require_once APPLICATION_PATH . '/models/Events.php';


class EventController  extends Zend_Controller_Action {
    public function indexAction()
    {

        $auth = Zend_Auth::getInstance ();

        $request = $this->getRequest ();
        $eventid = $this->_getParam('eventid');

        $this->view->create = new Form_Join( );
        $sports = Zend_Registry::get('sports');

        
        $this->emodel = new Model_Events();
        $this->view->event = $event = $this->emodel->get($eventid);
        $this->emodel->checkStatus();
        $this->view->eventid=$eventid;

        if (count($event["ps"])==2)
            $this->view->timerpos = 1;
        else
            $this->view->timerpos = 0;
        
        $time = isset($event["tm"])?$event["tm"]:array();
        if (count($time)>0){
            $this->view->part = count($time);
            $this->view->time = $time[$this->view->part-1];
        }else{
            $this->view->part = 0;
            $this->view->time = 0;
        }

        if (isset($event["as"])){
            $actions=$event["as"];
            $actions=array_reverse($actions);
            $this->view->actions = $actions;
        }

        $this->view->sport = $this->emodel->getSport();

        //$this->view->headScript()->appendFile( '/js/jquery-1.5.2.min.js');
        $this->view->headScript()->appendFile( '/js/event.js');

        /****statusTime******
         * 0->sin iniciar(faltan más de 15 minutos para empezar)
         * 1->sin iniciar, en la ventana para que empiece
         * 2..->iniciada parte (((n-2)/3)+1)(se puede pausar)
         * 3..->iniciada parte (((n-3)/3)+1)(se puede pausar y terminar)
         * 4..->terminada parte (((n-4)/3)+1), a la espera de iniciar parte (((n-4)/3)+2)
         */

        $this->view->finished=count($this->view->sport['parts'])<$this->view->part;

        if($this->view->finished){
            $this->view->statusTime=999;
        }else{
            if(!isset($event["tm"])){
                $eventDate=date('Ymdhi', $event['st']->sec);
                $currentDate=date('Ymdhi');
                $this->view->statusTime=($eventDate-15<=$currentDate)?1:0;
            }else{
                $this->view->statusTime=2+(($this->view->part-1)*3);
                //Si lleva más del 80% del tiempo de la parte ya se puede terminar
                if ($this->view->time>(($this->view->sport['parts']['part_'.($this->view->part-1)]['duration']*60)*0.8)){
                    $this->view->statusTime++;
                }
                if($this->view->time==0 && $this->view->part>0)
                    $this->view->statusTime+=2;
            }
        }
        
        $this->view->editable=false;
        if($auth->hasIdentity())
            $this->view->editable=($this->view->event['u']==$auth->getIdentity()->_id);
    }

    public function createAction()
    {
        $this->view->headScript()->appendFile('/js/jquery-ui-1.8.13.custom.min.js');
        $this->view->headScript()->appendFile('/js/jquery-ui-timepicker-addon.js');

        $auth = Zend_Auth::getInstance ();
        if (!$auth->hasIdentity()) $this->_redirect ( '/auth/login' );


        require_once APPLICATION_PATH . '/forms/createEvent.php';

        $request=$this->getRequest();
        $sports = Zend_Registry::get('sports');

        $form = new Form_CreateEvent();

        foreach ($sports as $sport) {
            $options[$sport["_id"]] = $sport["_id"];
        }
        $form->sport->options=$options;

        if ($request->isPost()){


            if ($form->isValid ( $request->getPost () )){


                $data = $form->getValues ();

                $event=new Model_Events();

                $startDate=$data['startDate'];
                $startTime=$data['startTime'];
                if(!Zend_Date::isDate($startDate)){
                    $this->view->error="invalid date";
                }
                
                if(!isset($this->view->error)){
                    //Si el evento no existe ya
                    if($event->exists($data['sport'], new MongoDate(strtotime($startDate." ".$startTime)), $data['competition'], $data['location'])){
                        $this->view->error="event already exists.";
                    }else{
                        $token=$event->create($auth->getIdentity()->_id,$data['sport'], new MongoDate(strtotime($startDate." ".$startTime)), $data['competition'], $data['location'], array($data['team_0'],$data['team_1']) , explode(",",$data['tags']));
                        if($token)
                            $this->_redirect ('/event/index/'.$token);
                        else
                            $this->view->error="error creating event.";
                    }
                }
            }
        }

        $this->view->formCreate = $form;

    }
}
